<?php

namespace admin\controllers;

use app\models\Banner;
use admin\components\AdminController;
use app\utils\BaseUtils;

class BannerController extends AdminController {

    public $name = '轮播图管理';
    public $pageSize = FALSE;

    public function actionIndex($type = Banner::TYPE_POST) {
        $searchModel = !$this->enableSearch ? NULL : new Banner();
        $query = Banner::find()->where(['type' => $type]);
        $dataProvider = $this->search($query, $searchModel);
        return $this->render(compact('searchModel', 'dataProvider'));
    }

    public function actionSave($id = FALSE) {
        /* @var $model Banner */
        $model = $this->findModel($id, Banner::className());
        if (!$model->load($this->post())) {
            return $this->render(compact('model'));
        }

        if (!empty($model->image)) {
            //BaseUtils::thumb(APP_ROOT . $model->image, 690, 360);
            BaseUtils::watermark(APP_ROOT . $model->image);
        }
        return $model->save();
    }

    public function actionDelete($id) {
        $model = Banner::findOne($id);
        if (!$model) {
            return $this->error('错误');
        }

        $file = APP_ROOT . '/' . $model->image;
        @unlink($file);
        return $model->delete();
    }
}
